package org.scc.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.scc.exception.SCCException;
import org.scc.model.User;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	protected int page;//分页参数，页数
	protected int rows;//分页参数，每页行数
	protected int id;
	protected String ids;//页面选择的id串，逗号分割
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	/**
	 * 取得根据分页参数计算出来的开始记录位置
	 * @return
	 */
	public int getRowBeginIndex(){
		return ( page - 1 ) * rows;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * 使用response的PrintWriter向前台写信息
	 * @param content
	 */
	public void responeString(String content){
		try {
			HttpServletResponse resp = ServletActionContext.getResponse();
			resp.setCharacterEncoding("UTF-8");
			PrintWriter out = resp.getWriter();
			out.write(content);
			out.flush();
			out.close();
		} catch (IOException e) {
			throw new SCCException("向客户端写信息出错，请联系管理员！");
		}
	}
	
	/**
	 * 向前台写success
	 */
	public void responseSuccess(){
		responeString(SUCCESS);
	}
	
	public User getCurrentUser(){
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		User currentUser = (User) session.get("user");
		return currentUser;
	}
	
}
